﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Text;



namespace ReconUI
{
    public partial class FileUpload : System.Web.UI.Page
    {
        //Initialize mysql connection

        //MySqlConnection connection = new MySqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        //MySqlCommand cmd;

        //MySqlDataAdapter adapter;
        //DataTable DTItems;

        private string msg = "";
        private string str = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (((UserVO)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                //1.Insert file detail   2.Get File Detail   3.

                //List<string> fileLines = File.ReadAllLines("F:\file.txt").ToList();
                HttpPostedFile fileBrowse = fuInvoiceState.PostedFile;
                string strFileExt = Path.GetExtension(fuInvoiceState.FileName);
                string strFilename = Path.GetFileName(fuInvoiceState.FileName);
                int filtLength = fuInvoiceState.FileBytes.Length;
                Object fileId;
                if (strFilename != "" && filtLength > 0)
                {

                    //Code for Insert file detail into pro_file_upload_details table              
                    //fileId =  1111111001;//System.DateTime.Now.ToString("ddMMyyyyhhmmss") + "001";
                    string fileType = ddlFileType.SelectedValue;
                    string ss = Server.MapPath(".") + @"\" + strFilename + ".txt";
                    fuInvoiceState.PostedFile.SaveAs(Server.MapPath(".") + @"\" + strFilename + ".txt");
                    string OpenPath = Server.MapPath(".") + @"\" + strFilename + ".txt";
                    string FILENAME = OpenPath;

                    //Tempory assignment of value to object


                    UploadHelper objUploadHelper = new UploadHelper();
                    UserVO currentUser = (UserVO)Session["CurrentUserVO"]; //UploadHelper.getTestUser(); 
                    int retval = objUploadHelper.ParseUploadedFile(currentUser, fileType, strFilename, strFileExt, FILENAME);
                    
                    if (retval == 2)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('File already uploadeded!')</script>");
                        lblMessage.Text = "File already uploadeded!";
                        btnClickPopup.Show();

                    }
                    else if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('File uploaded successfully!')</script>");
                        lblMessage.Text = "File uploaded successfully!";
                        btnClickPopup.Show();
                        
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error in File upload!')</script>");
                        lblMessage.Text = "Error in File upload!";
                        btnClickPopup.Show();
                    }
                }//End of  using (StreamReader reader = File.OpenText(FILENAME))

                else
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Check the file size!')</script>");
                    lblMessage.Text = "Check the file size!";
                    btnClickPopup.Show();
                }

            }

            catch (Exception ex)
            {
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Error in File upload!')</script>");
                lblMessage.Text = "Error in File upload!" + ex.ToString();
                btnClickPopup.Show();
            }



        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }
    }
}